﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using MinistryStandardPlatformEntity;
using MinistryStandardPlatformWeb.Models.ReportCenter;

namespace MinistryStandardPlatformWeb.Controllers.Reports
{
    [MenuAuthorized("42")]
    public class TransDetailController : Controller
    {
        private MSPDataContext context = new MSPDataContext();
        //
        // GET: /TransDetail/

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult GetData(string startTime, string endTime, List<string> vehicleNums)
        {
            if (!string.IsNullOrEmpty(startTime) && !string.IsNullOrEmpty(endTime) && vehicleNums != null && vehicleNums.Count != 0)
            {
                List<TransDetail> list = (from t in this.context.TranTaskListDetail
                                          join r in this.context.TranTaskList
                                          on t.tranTaskListId equals r.taskListId
                                          where vehicleNums.Contains(t.vehicleNum)
                                          && r.insertTime.CompareTo(startTime) >= 0 && r.insertTime.CompareTo(endTime) <= 0
                                          select new TransDetail()
                                          {
                                              mTaskId = r.taskListId,
                                              mTaskTime = r.insertTime,
                                              mTransDistance = r.transDistance.ToString(),
                                              mVehicleNum = t.vehicleNum,
                                              mTransNum = t.transPerCap,
                                              mLeaveStationTime = t.outStartRegTime,
                                              mArriveSiteTime = t.inEndRegTime,
                                              mLeaveSiteTime = t.outEndRegTime,
                                              mRetStationTime = t.inStartRegTime
                                          }).ToList();
                var json = new
                {
                    total = list.Count,
                    rows = list
                };
                var serializer = new JavaScriptSerializer();
                serializer.MaxJsonLength = Int32.MaxValue;
                return Content(serializer.Serialize(json), "application/json");
            }
            return null;
        }

        [HttpPost]
        [MenuAuthorized("1", Order = 1)]
        public ActionResult ExportExcel()
        {
            List<TransDetail> list = CommonFunctions.JsonStrToObject<List<TransDetail>>(Request.Form["data"]);
            var sbHtml = new System.Text.StringBuilder();
            sbHtml.Append("<table border='1' cellspacing='0' cellpadding='0'>");
            sbHtml.Append("<tr>");
            var lstTitle = new List<string> { "序号", "任务单号", "项目名称", "派单时间", "运输距离","车牌号", "所属单位", "运输方量","出站时间","到工地时间","出工地时间","返站时间","运输耗时" };
            foreach (var item in lstTitle)
            {
                sbHtml.AppendFormat("<td style='font-size: 14px;text-align:center;background-color: #DCE0E2; font-weight:bold;' height='25'>{0}</td>", item);
            }
            sbHtml.Append("</tr>");

            for (int i = 0; i < list.Count; i++)
            {
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat("<td>{0}</td>", i + 1);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mTaskId);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mProjectName);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mTaskTime);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mTransDistance);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mVehicleName);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mDepart);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mTransNum);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mLeaveStationTime);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mArriveSiteTime);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mLeaveSiteTime);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mRetStationTime);
                sbHtml.AppendFormat("<td>{0}</td>", list[i].mTotalTime);
                sbHtml.Append("</tr>");
            }
            sbHtml.Append("</table>");

            //第一种:使用FileContentResult
            byte[] fileContents = System.Text.Encoding.Default.GetBytes(sbHtml.ToString());
            return File(fileContents, "application/ms-excel", "运输明细.xls");

            ////第二种:使用FileStreamResult
            //var fileStream = new MemoryStream(fileContents);
            //return File(fileStream, "application/ms-excel", "fileStream.xls");

            ////第三种:使用FilePathResult
            ////服务器上首先必须要有这个Excel文件,然会通过Server.MapPath获取路径返回.
            //var fileName = Server.MapPath("~/Files/fileName.xls");
            //return File(fileName, "application/ms-excel", "fileName.xls");
            //return null;
        }
    }
}
